﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.TimeSlots
{
    public partial class AddTimeSlot : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["AddTimeSlotLabeltext"] != null && IsPostBack)
            {
                AddTimeSlotLabel.Text = (string)HttpContext.Current.Session["AddTimeSlotLabeltext"];
                //OKButton.Enabled = false;
            }
            else if (IsPostBack)
            {
                AddTimeSlotLabel.Text = "";
            }
            else
            {
                AddTimeSlotLabel.Text = "";
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/TimeSlots/ListTimeSlots.aspx");
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton timeslotsMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("TimeSlotsMenuButton");
            timeslotsMenuButton.CssClass = "selected";

            if (AddTimeSlotLabel.Text != "") return;
            AddTimeSlotLabel.Text = "Input information for new Time Slot";
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            String strstarttime = inputstarttime.Text;
            String strlength = inputlength.Text;
            int starttime=-1;
            int length;

            
            if (!int.TryParse(strstarttime, out starttime)||starttime<0||starttime>2400||starttime%100>=60)
            {
                String errmsg = "Start time must be entered in 4 digit 24hr time";
                if (AddTimeSlotLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddTimeSlotLabeltext"] = errmsg;
                    Server.Transfer("~/TimeSlots/AddTimeSlot.aspx", true);
                }
                return;
            }
            cs499.Database.DayOfWeek days=0;
            if (inputDays.Items[0].Selected) days = days | Database.DayOfWeek.Monday;
            if (inputDays.Items[1].Selected) days = days | Database.DayOfWeek.Tuesday;
            if (inputDays.Items[2].Selected) days = days | Database.DayOfWeek.Wednesday;
            if (inputDays.Items[3].Selected) days = days | Database.DayOfWeek.Thursday;
            if (inputDays.Items[4].Selected) days = days | Database.DayOfWeek.Friday;
            if (inputDays.Items[5].Selected) days = days | Database.DayOfWeek.Saturday;
            if (inputDays.Items[6].Selected) days = days | Database.DayOfWeek.Sunday;
            if (days == 0)
            {
                String errmsg = "At least one day must be selected";
                if (AddTimeSlotLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddTimeSlotLabeltext"] = errmsg;
                    Server.Transfer("~/TimeSlots/AddTimeSlot.aspx", true);
                }
                return;
            }

            if (!int.TryParse(strlength, out length))
            {
                String errmsg = "length must be a number greater than 0";
                if (AddTimeSlotLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddTimeSlotLabeltext"] = errmsg;
                    Server.Transfer("~/TimeSlots/AddTimeSlot.aspx", true);
                }
                return;
            }

            TimeSlot newtimeslot = new TimeSlot();
            newtimeslot.Days = days;
            newtimeslot.StartTime = new TimeSpan(starttime / 100, starttime % 100, 0);
            newtimeslot.Length = length;
            
            if (ListOfDepartment.TimeSlotExists(newtimeslot))
            {
                String errmsg = "This time slot already exists in the database";
                if (AddTimeSlotLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddTimeSlotLabeltext"] = errmsg;
                    Server.Transfer("~/TimeSlots/AddTimeSlot.aspx", true);
                }
                return;
            }

            ListOfDepartment.CreateTimeSlot(newtimeslot);

            HttpContext.Current.Session["AddTimeSlotLabeltext"] = "";
            AddTimeSlotLabel.Text = "Success";
            Response.Redirect("~/TimeSlots/ListTimeSlots.aspx?action=success");

        }
    }
}